<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manufacturer extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('product/product_model');
    }

    function index(){
        $data['result'] = $this->product_model->get_manufacturer();
        $data['tpl'] = 'admin/manufacturer/index';
        $this->load->view('_layout/admin', $data);
    }

    function add(){
        $this->_update();
    }

    function edit($id= 0 ){
        $this->update($id);
    }

    function update($id = 0){
        $id = intval($id);

        if ($_POST){
            $info = array(
                'title'        => trim($this->input->post('title')),
                'source'       => trim($this->input->post('source')),
                'update_date'  => date('Y-m-d H:i:s')
            );

            if ($id != 0){
                $info['manufacture_id'] = $id;
                $info['update_date'] = date('Y-m-d H:i:s');
            } else {
                $info['active_flag'] = ACTIVE_FLAG;
                $info['delete_flag'] = UNDELETE_FLAG;
                $info['create_date'] = date('Y-m-d H:i:s');
            }

            $result = $this->product_model->update_manufacturer($info);
            if (!is_bool($result)){
                $data['msg'] = 'Lưu thương hiệu thành công';
            }
        }

        if ($id != 0) {
            $data['data'] = $this->product_model->get_manufacturer($id);
        }

        $data['tpl']      = 'admin/manufacturer/edit';
        $this->load->view('_layout/admin', $data);
    }

    function delete($id){
        $id = intval($id);
        if (!is_array($id)){
            $id = array($id);
        }
        $result = $this->product_model->_delete_manufacturer($id);
        redirect('admin/manufacturer');
    }
}